package bsw.wode.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

import bsw.wode.action.AuthEnabled;
import bsw.wode.config.ActionConstant;
import bsw.wode.config.SessionConstant;

public class CheckcodeInterceptor implements Interceptor {

	private static final long serialVersionUID = -4752821948154640998L;

	public void destroy() {
	}

	public void init() {
	}

	public String intercept(ActionInvocation ai) throws Exception {
		Map session = ai.getInvocationContext().getSession();
		String checkCode = (String) session.get(SessionConstant.CHECK_CODE);
		// ai.get
		if (checkCode == null) {
			Action action = (Action) ai.getAction();
			if (action instanceof AuthEnabled) {
				if (!((AuthEnabled) action).hasPermission()) {
					return ActionConstant.NOPERMISSION;
				}
			}
		}

		return ai.invoke();
	}

}